package graph.impl;

import java.util.Iterator;
import java.util.LinkedList;

import edu.princeton.cs.introcs.In;
import graph.design.Graph;
import graph.design.Vertex;

class SimpleGraph<E extends Comparable<E>> implements Graph<E>	{
	
	protected int vertexCount = 0;
	
	private LinkedList<Vertex<E>> vertexList = null;
	
	SimpleGraph() {
		vertexList = new LinkedList<Vertex<E>>();
	}
	
	public Iterator<Vertex<E>> getVertices() {
		return vertexList.iterator();
	}

	public boolean deleteVertex(Vertex<E> v) {
		boolean deleted = false;
		deleted = vertexList.remove(v);
		
		if(deleted){
			this.vertexCount--;
		}
		return deleted;
	}

	@Override
	public int vertexCount() {		
		return vertexCount;
	}

	@Override
	public boolean addEdge(Vertex<E> v1, Vertex<E> v2) {		
		return v1.addEdgeTo(v2);		
	}

	@Override
	public void addVertex(Vertex<E> v) {		
		this.vertexList.add(v);
	}
	
	public String toString(){
		
		StringBuilder builder = new StringBuilder();
		String NEWLINE = System.getProperty("line.separator");
		
		for (Vertex<E> v : vertexList) {
			builder.append(v.getVertex() + " --> " );
			builder.append(v);
			builder.append(NEWLINE);
		}
		return builder.toString();
	}
	
	public static void main(String[] args) {
		
		VertexImpl<Integer> v1 = new VertexImpl<Integer>(1);
		VertexImpl<Integer> v2 = new VertexImpl<Integer>(2);
		VertexImpl<Integer> v3 = new VertexImpl<Integer>(3);
		SimpleGraph<Integer> graph = new SimpleGraph<Integer>();
		
		graph.addVertex(v1);
		graph.addVertex(v2);
		graph.addVertex(v3);
		
		graph.addEdge(v1,v2);
		graph.addEdge(v1,v3);

		graph.addEdge(v2,v1);
		graph.addEdge(v2,v3);

		graph.addEdge(v3,v1);
		graph.addEdge(v3,v2);

		System.out.println(graph.toString());
		
	}
}
